package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.Banner;
import com.ysd.util.DBManager;

public class BannerDaoImpl {
	
	public List<Banner> getBanner(){
		//查询多条,使用集合
		//访问修饰符 返回值类型 方法名 (参数列表) {方法体}
		List<Banner> list = new ArrayList<Banner>();//创建集合
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {//正常
			//创建Statement对象
			st = conn.createStatement();
			//创建并执行sql语句
			rs = st.executeQuery("select * from banner");
			//遍历结果集
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String banberImg = rs.getString("banberImg");
				Banner b = new Banner(bannerId, banberImg);
				list.add(b);
			}
			return list;
		} catch (SQLException e) {//错误
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	} 
	
	public List<Banner> getBannerByPage(int page,int size){
		//查询多条,使用集合
		//访问修饰符 返回值类型 方法名 (参数列表) {方法体}
		List<Banner> list = new ArrayList<Banner>();//创建集合
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {//正常
			//创建Statement对象
			st = conn.createStatement();
			//创建并执行sql语句
			String sql = "select * from banner limit "+(page-1)*size+","+size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String banberImg = rs.getString("banberImg");
				Banner b = new Banner(bannerId, banberImg);
				list.add(b);
			}
			return list;
		} catch (SQLException e) {//错误
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	} 
	
	/**
	 * 根据id删除轮播图
	 * @param banId
	 * @return 受影响行数
	 */
	public int delAdminBanner(int banId) {
		return DBManager.executeUpdate("delete from banner where bannerId=" + banId);
	}
	
	public int addBanner(Banner ba) {
		return DBManager.executeUpdate("insert into banner values(null,'"+ba.getBanberImg()+"')");
	}
	
	/**
	 * 根据轮播图id查询数据
	 * @param banId
	 * @return
	 */
	public Banner selBanner(int banId) {
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		Banner ban = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from banner where bannerId="+banId);
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String bannerImg = rs.getString("banberImg");
				ban = new Banner(bannerId, bannerImg);
			}
			return ban;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据轮播图id修改数据
	 * @param ban
	 * @return
	 */
	public int updateBanById(Banner ban) {
		String sql = "update banner set banberImg='"+ban.getBanberImg()+"' where bannerId="+ban.getBannerId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return 多个id为String
	 */
	public int delBannerByIds(String ids) {
		String sql = "delete from banner where bannerId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
